import Loading from 'element-ui/lib/loading';

export default function(options) {
  const target = options.target || false;
  const config = {
    target: options.target,
    body: !!target,
    fullscreen: !target,
    lock: true,
    text: options.text || '加载中...',
    spinner: 'el-icon-loading',
    background: options.background || 'rgba(255, 255, 255, 0.8)'
  };

  return function(target, propertyKey, descriptor) {
    const method = descriptor.value;
    descriptor.value = async function(...args) {
      const that = this._isVue ? this : target;
      const loadingInstance = Loading.service(config);
      await method.apply(that, args);
      this.$nextTick(() => {
        loadingInstance.close();
      });
    };
  };
}
